const clickEvent = (function () {
    if ('ontouchstart' in document.documentElement)
        return 'touchstart';
    else
        return 'click';
})();

$(function () {
    getInfo();
});

function getInfo() {
    zn_ajax("/index/manage/getManageInfo", {}, function (e) {
        if (e.code === 1) {
            console.log(e)
            let html = '';
            let main = $(".main");
            main.html('');
            for (let i = 0; i < e.data.length; i++) {
                html += addHtml(e.data[i]);
            }
            main.html(html);
        } else {
            msg(e.msg);
        }
    });
}

function addHtml(e) {
    let html = `
    <div class="item">
        <div class="item-head">
            <div class="item-head-left">
                <div><img src="${e.avatar}" alt=""></div>
                <div>
                    <span>ID: ${e.id}</span>
                    <span>昵称: ${e.nickname}</span>
                </div>
            </div>

            <div class="item-head-right">
                <span>${e.explain}</span>
            </div>
        </div>
        <div class="item-main" data-action="${e.id}">
            <div data-name='d_k' data-action="${e.d_k}">
                <div>点控</div>
                <div><img src="/files/images/center/${e.d_k}.png" alt=""></div>
            </div>
            <div data-name='g_b' data-action="${e.g_b}">
                <div>智能防反</div>
                <div><img src="/files/images/center/${e.g_b}.png" alt=""></div>
            </div>
            <div data-name='x_c' data-action="${e.x_c}">
                <div>巡查</div>
                <div><img src="/files/images/center/${e.x_c}.png" alt=""></div>
            </div>
        </div>
    </div>
`;
    return html;
}

function zn_ajax(url, data, callback) {
    data['__token__'] = token;
    $.ajax({
        type: "post",
        url: JONO_HOST + url,
        dataType: 'JSON',
        data: data,
        async: false,
        success: callback,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            msg(errorThrown);
        },
        beforeSend : function(request) {
            request.setRequestHeader("Authorization",window.localStorage.getItem("token"));
        },
    });
}

function msg(msg) {
    let tip = $(".msg");
    tip.html(msg);
    tip.show();
    setTimeout(function () {
        tip.hide();
    }, 3000);
}

function initOn() {
    setTimeout(function () {
        on = 0;
    }, 1000);
}

function initOn_1() {
    setTimeout(function () {
        on_1 = 0;
    }, 1000);
}


function show_pop_up(data) {
    let pop_up = $(".pop-up > .pop-up-msg");
    pop_up.html('');
    let html = `
        <div class="pop-up-msg-head">请检查用户信息</div>
            <div class="pop-up-msg-main">
            <div><img src="${data.avatar}" alt=""></div>
            <span>ID: ${data.id}</span>
        <span>昵称: ${data.nickname}</span>
        </div>
        <div class="pop-up-msg-button">
            <button onclick="openAgent(${data.id})">确定</button>
            <button onclick="hide_pop_up()">取消</button>
        </div>
    `;
    pop_up.html(html);
    $(".pop-up").show();
}

function hide_pop_up() {
    $(".pop-up").hide();
}

let on = 0;
$(document).on(clickEvent, ".input button", function () {
    if (on === 1) {
        console.log('请勿重复操作')
        return false;
    }
    on = 1;
    initOn();
    let uid = $(".input input").val().trim();
    if (uid.length <= 0) {
        msg('请输入用户ID');
        return false;
    }
    zn_ajax("/index/manage/GetUser", {
        'uid': uid
    }, function (e) {
        if (e.code === 1) {
            $(".input input").val('');
            show_pop_up(e.data);
        } else {
            msg(e.msg);
        }
    });
});
let on_1 = 0;
function openAgent(uid) {
    hide_pop_up();
    if (on_1 === 1) {
        console.log('请勿重复操作')
        return false;
    }
    on_1 = 1;
    initOn_1();
    zn_ajax("/index/manage/openAgent", {
        'uid': uid
    }, function (e) {
        if (e.code === 1) {
            getInfo();
            $(".input input").val('');
            let day_card = $('#num');
            let num = day_card.text();
            day_card.text((num-1));
            msg(e.msg);
        } else {
            msg(e.msg);
        }
    });
}
$(document).on(clickEvent, ".item-main > div", function () {
    let name = $(this).attr('data-name');
    let action = $(this).attr('data-action');
    let uid = $(this).parent().attr('data-action');
    console.log(action)
    zn_ajax("/index/manage/save", {
        'uid': uid,
        'name': name,
        'action': action
    }, (e) => {
        if (e.code === 1) {
            $(this).attr('data-action', e.data.action);
            $(this).children('div').children().attr('src', "/files/images/center/" + e.data.action + ".png");
            msg(e.msg);
        } else {
            msg(e.msg);
        }
    });
});

